package com.campusexpresspickup.config;

import com.campusexpresspickup.entity.OauthUser;
import com.campusexpresspickup.entity.UserResource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * @date 2021-11-16 14:10
 */
public class OauthUserDetails implements UserDetails {

  private OauthUser oauthUser;

  private List<UserResource> userResourceList;

  public OauthUserDetails(OauthUser oauthUser,
      List<UserResource> userResourceList) {
    this.oauthUser = oauthUser;
    this.userResourceList = userResourceList;
  }

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    return userResourceList.stream()
        .map(role -> new SimpleGrantedAuthority(role.getId() + ":" + role.getName())).collect(
            Collectors.toList());
  }

  @Override
  public String getPassword() {
    return oauthUser.getPassword();
  }

  @Override
  public String getUsername() {
    return oauthUser.getUserName();
  }

  @Override
  public boolean isAccountNonExpired() {
    return true;
  }

  @Override
  public boolean isAccountNonLocked() {
    return true;
  }

  @Override
  public boolean isCredentialsNonExpired() {
    return true;
  }

  @Override
  public boolean isEnabled() {
    return oauthUser.getStatus().equals(false);
  }
}
